package com.gitee.base.modules.core.controller;

import com.gitee.base.modules.BaseController;
import com.gitee.base.modules.core.model.SysPermExt;
import com.gitee.base.modules.core.service.ISysPermExtService;
import com.gitee.base.security.permission.Permission;
import com.gitee.base.security.permission.PermissionResolver;
import com.gitee.base.security.permission.annotation.RequiresPermissionsDesc;
import com.gitee.base.utils.JsonVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/core/menu")
public class CoreMenuController extends BaseController {
    @Autowired
    private PermissionResolver permissionResolver;
    @Autowired
    private ISysPermExtService sysPermExtService;

    @RequiresPermissionsDesc(folder = "系统管理", menu = "菜单管理", button = "排序调整",page = true)
    @RequiresPermissions("core:menu:list")
    @GetMapping("/list")
    public String list(ModelMap modelMap){
        List<Permission> permissionList = this.permissionResolver.permissions();
        List<SysPermExt> sysPermExtList = this.sysPermExtService.list();
        if(sysPermExtList==null) sysPermExtList = new ArrayList<>();

        List<SysPermExt> waitFillList = new ArrayList<>();
        for(Permission folder:permissionList){
            Optional<SysPermExt> optionalFolder = sysPermExtList.stream().filter(s->s.getId().equals(folder.getId())).findAny();
            if(!optionalFolder.isPresent()){
                waitFillList.add(SysPermExt.builder().id(folder.getId()).path(folder.getName()).sort(10000).build());
            }

            if(folder.getChildren()!=null){
                for(Permission menu:folder.getChildren()){
                    Optional<SysPermExt> optionalMenu = sysPermExtList.stream().filter(s->s.getId().equals(menu.getId())).findAny();
                    if(!optionalMenu.isPresent()){
                        waitFillList.add(SysPermExt.builder().id(menu.getId()).pid(folder.getId()).path(folder.getName()+" > "+menu.getName()).sort(10000).build());
                    }
                }
            }
        }

        if(!waitFillList.isEmpty()){
            this.sysPermExtService.saveBatch(waitFillList);
            sysPermExtList.addAll(waitFillList);
        }

        modelMap.put("sysPermExtList", sysPermExtList.stream().sorted(Comparator.comparingInt(SysPermExt::getSort)).collect(Collectors.toList()));
        return "core/menu/list";
    }

    @RequiresPermissions("core:menu:list")
    @PostMapping("/up")
    @ResponseBody
    public JsonVo up(@NotBlank String id){
        SysPermExt sysPermExt = this.sysPermExtService.getById(id);
        if(sysPermExt==null){
            return JsonVo.fail(404,"想要移动的数据不存在");
        }
        LambdaQueryWrapper<SysPermExt> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(sysPermExt.getPid()==null){
            lambdaQueryWrapper.isNull(SysPermExt::getPid);
        }else{
            lambdaQueryWrapper.eq(SysPermExt::getPid,sysPermExt.getPid());
        }
        lambdaQueryWrapper.le(SysPermExt::getSort,sysPermExt.getSort());
        lambdaQueryWrapper.orderByDesc(SysPermExt::getSort);
        lambdaQueryWrapper.last(" limit 1");

        SysPermExt prevSysPermExt = this.sysPermExtService.getOne(lambdaQueryWrapper);
        if(prevSysPermExt==null){
            return JsonVo.fail(404,"当前数据已经在最顶部");
        }

        List<SysPermExt> sysPermExts = new ArrayList<>();
        sysPermExts.add(SysPermExt.builder().id(sysPermExt.getId()).sort(prevSysPermExt.getSort()).build());
        sysPermExts.add(SysPermExt.builder().id(prevSysPermExt.getId()).sort(sysPermExt.getSort()+1).build());
        this.sysPermExtService.updateBatchById(sysPermExts);

        return JsonVo.success();
    }

    @RequiresPermissions("core:menu:list")
    @PostMapping("/down")
    @ResponseBody
    public JsonVo down(@NotBlank String id){
        SysPermExt sysPermExt = this.sysPermExtService.getById(id);
        if(sysPermExt==null){
            return JsonVo.fail(404,"想要移动的数据不存在");
        }
        LambdaQueryWrapper<SysPermExt> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(sysPermExt.getPid()==null){
            lambdaQueryWrapper.isNull(SysPermExt::getPid);
        }else{
            lambdaQueryWrapper.eq(SysPermExt::getPid,sysPermExt.getPid());
        }
        lambdaQueryWrapper.ge(SysPermExt::getSort,sysPermExt.getSort());
        lambdaQueryWrapper.orderByAsc(SysPermExt::getSort);
        lambdaQueryWrapper.last(" limit 1");
        SysPermExt nextSysPermExt = this.sysPermExtService.getOne(lambdaQueryWrapper);
        if(nextSysPermExt==null){
            return JsonVo.fail(404,"当前数据已经在最顶部");
        }

        List<SysPermExt> sysPermExts = new ArrayList<>();
        sysPermExts.add(SysPermExt.builder().id(sysPermExt.getId()).sort(nextSysPermExt.getSort()+1).build());
        sysPermExts.add(SysPermExt.builder().id(nextSysPermExt.getId()).sort(sysPermExt.getSort()).build());
        this.sysPermExtService.updateBatchById(sysPermExts);

        return JsonVo.success();
    }
}
